import os
from datetime import datetime
import subprocess

# 创建日志目录
log_dir = "logs"
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

# 生成当天的日志文件名
log_file = os.path.join(log_dir, f"application_{datetime.now().strftime('%Y%m%d')}.log")

# 启动服务
cmd = f"uvicorn app.main:app --host 0.0.0.0 --port 8000"
with open(log_file, "a") as f:
    process = subprocess.Popen(
        cmd.split(),
        stdout=f,
        stderr=subprocess.STDOUT,
        universal_newlines=True
    )

# 将进程ID写入文件
with open("service.pid", "w") as f:
    f.write(str(process.pid))
